<?php
namespace Foam\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;

class CarteProduitTable extends AbstractTableGateway
{
    protected $table ='foam_carte_produit';
    
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new CarteProduit());
        $this->initialize();
    }
    
    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet;
    }
    
    public function getCarteProduit($id_carte)
    {
        $id_carte  = (int) $id_carte;
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('id_produit'));
        $select->from($this->table);
        
        $where = new Where();
        $where->equalTo('id_carte', $id_carte);
        $where->equalTo('direction', 1);
        $select->where($where);
        
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        return $result;
    }
    
    public function getCarteSynthese($latestCartes)
    {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array());
        $select->from(array('cprd' => $this->table));
        $select->join(array('prd' => 'foam_produit'), 
                        'prd.id_produit = cprd.id_produit',
                        array('libelle'));
        $select->join(array('c' => 'foam_carte'), 
                        'c.id_carte = cprd.id_carte',
                        array('clibelle' => 'libelle'));
        $select->join(array('f' => 'foam_famille'), 
                        'f.id_famille = prd.id_famille',
                        array('flibelle' => 'libelle'));
        $select->join(array('sf' => 'foam_sous_famille'), 
                        'sf.id_ssfamille = prd.id_ssfamille',
                        array('ssflibelle' => 'libelle'));
        
        $where = new Where();
        $where->in('c.libelle', $latestCartes);
        $where->equalTo('direction', 1);
        $select->where($where);

        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        return $result;
    }
    
    public function getCarteDetail($id_carte)
    {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('direction'));
        $select->from(array('cprd' => $this->table));
        $select->join(array('prd' => 'foam_produit'), 
                        'prd.id_produit = cprd.id_produit',
                        array('libelle'));
        $select->join(array('f' => 'foam_famille'), 
                        'f.id_famille = prd.id_famille',
                        array('flibelle' => 'libelle'));
        
        $where = new Where();
        $where->equalTo('cprd.id_carte', $id_carte);
        $select->where($where);

        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        return $result;
    }
    
    public function attribCarteProduit($id_crt_prd, $id_produit, $id_carte, $direction ,$date)
    {
        $data = array(
            'id_crt_prd' => $id_crt_prd,
            'id_produit' => $id_produit,
            'id_carte' => $id_carte,
            'direction' => $direction,
            'date' => $date,
            
        );
        
        $this->insert($data);
    }
    
    public function updateCarteProduit($id_produit, $id_carte, $direction ,$date)
    {
        $data = array(
            'direction' => $direction,
            'date' => $date,
            
        );
        
        $this->update($data, array('id_produit' => $id_produit, 'id_carte' => $id_carte));
    }
}
